package com.feinno.fpa;

import com.feinno.database.DataRow;
import com.feinno.database.DataTable;
import com.feinno.database.Database;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by Lei Gao on 8/13/15.
 */
public class FpaFunction {
	int id;
	int dependencyId;
	String module;
	String packageName;
	String className;
	String method;

	List<FpaParameter> parameters = new ArrayList<>();
	FpaParameter returnType;

	List<FpaFunction> references = new ArrayList<>();

	public String getKey() {
		return packageName + "." + className + "::" + method;
	}

	public FpaType getType() {
		if (method.contains("get")) {
			return FpaType.EQ;
		}
		if (method.contains("save") || method.contains("create") || method.contains("insert")) {
			return FpaType.EI;
		}
		return FpaType.EO;
	}

	public void fillParameters(Database db) throws SQLException {
		String sql = "select * from ParameterDoc p inner join JavadocDictionary j on p.javadocid = j.id " +
				"where className = ? and method = ?";
		DataTable table = db.executeTable(sql, packageName + "." + className, method);
//		if (table.getRowCount() == 0) {
//			System.out.printf("ZERO package = %s method = %s\n",  packageName + "." + className, method);
//		}

		for (DataRow row: table.getRows()) {
			FpaParameter p = new FpaParameter();
			p.name = row.getString("parameter");
			p.type = row.getString("parameterType");
			parameters.add(p);
		}
	}

//	public List<FpaResult> expands(FpaResult r0, FpaDictionary dict) {
//		List<FpaResult> results = new ArrayList<>();
//		for (FpaFunction ref: references) {
//			FpaExpand expand = dict.getExpand(ref.getKey());
//			if (expand != null) {
//				FpaResult r = r0.clone();
//				r.function = expand.getExpand();
//			}
//		}
//	}
}
